package com.ilancuo.thinkinjava.chapter_05;

/**
 * 实例初始化 - 初始化每一个对象的非静态变量
 * 可以保证无论使用哪个显示构造器，某些操作都会发生，在构造器之前执行
 */
class Mug {
    Mug(int marker) {
        System.out.println("Mug(" + marker + ")");
    }

    void f(int marker) {
        System.out.println("f(" + marker + ")");
    }
}

public class Mugs {
    Mug mug1;
    Mug mug2;

    {
        mug1 = new Mug(1);
        mug2 = new Mug(2);
        System.out.println("mug1 & mug2 initialized");
    }

    Mugs() {
        System.out.println("Mugs()");
    }

    Mugs(int marker) {
        System.out.println("Mugs(int)");
    }

    public static void main(String[] args) {
        System.out.println("Inside main()");
        new Mugs();
        System.out.println("new Mugs() completed");
        new Mugs(1);
        System.out.println("new Mugs(int) completed");
    }
}
//Outputs
/**
 * Inside main()
 * Mug(1)
 * Mug(2)
 * mug1 & mug2 initialized
 * Mugs()
 * new Mugs() completed
 * Mug(1)
 * Mug(2)
 * mug1 & mug2 initialized
 * Mugs(int)
 * new Mugs(int) completed
 */
